<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-form-item :label="$t('债券一级类别')" prop="primaryBondCategory" :rules="[{ required: true, validator: checkIsNull, trigger: ['blur', 'change'] }]">
          <dictSelect
            v-model="bondForm.primaryBondCategory"
            :clearable="true"
            dictionary="deal_bond_NW"
            @change="setBondCategory"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item :label="$t('债券二级类别')" prop="secondaryBondCategory" :rules="[{ required: true, validator: checkIsNull, trigger: ['blur', 'change'] }]">
          <dictSelect
            v-if="bondCategoryTwoStatus"
            v-model="bondForm.secondaryBondCategory"
            :clearable="true"
            :dictionary="'deal_bond_NW_' + bondForm.primaryBondCategory"
            @change="formatSecondary($event)"
          />
        </el-form-item>
      </el-col>
      <!-- 债券代码 -->
      <el-col v-if="hidePrivate.includes('bondCode')" :span="12">
        <el-form-item :label="$t('债券代码')" prop="bondCode" :rules="[{ required: true, validator: checkIsNull, trigger: ['blur', 'change'] }]">
          <!-- <el-input v-model="bondForm.bondCode" :placeholder="$t('债券代码')" clearable show-word-limit :maxlength="100" />-->
          <el-autocomplete
            v-model="bondForm.bondCode"
            popper-class="my-autocomplete"
            :fetch-suggestions="querySearch"
            :placeholder="$t('请选择债券代码')"
            @select="handleSelect"
          >
            <i class="el-icon-time" />
            <template slot-scope="{ item }">
              <div class="name">{{ item.bondCode }}</div>
              <span class="addr">{{ item.bondName }}</span>
            </template>
          </el-autocomplete>
        </el-form-item>
      </el-col>
      <!-- 债券名称 -->
      <el-col v-if="hidePrivate.includes('bondName')" :span="12">
        <el-form-item :label="$t('债券名称')" prop="bondName" :rules="[{ required: true, validator: checkIsNull, trigger: ['blur', 'change'] }]">
          <el-input v-model="bondForm.bondName" :placeholder="$t('债券名称')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- financingSubject	融资主体 -->
      <el-col v-if="hidePrivate.includes('financingSubject')" :span="12">
        <el-form-item :label="$t('融资主体')" prop="financingSubject">
          <el-input v-model="bondForm.financingSubject" :placeholder="$t('融资主体')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- obligor	债务人 -->
      <el-col v-if="hidePrivate.includes('obligor')" :span="12">
        <el-form-item :label="$t('债务人')" prop="obligor">
          <el-input v-model="bondForm.obligor" :placeholder="$t('债务人')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- thirdPartyPrincipal	第三方委托人 -->
      <el-col v-if="hidePrivate.includes('thirdPartyPrincipal')" :span="12">
        <el-form-item :label="$t('第三方委托人')" prop="thirdPartyPrincipal">
          <el-input v-model="bondForm.thirdPartyPrincipal" :placeholder="$t('第三方委托人')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- countryRisk	风险所在国 -->
      <el-col v-if="hidePrivate.includes('countryRisk')" :span="12">
        <el-form-item :label="$t('风险所在国')" prop="countryRisk">
          <el-input v-model="bondForm.countryRisk" :placeholder="$t('风险所在国')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- subjectName	标的名称 -->
      <el-col v-if="hidePrivate.includes('subjectName')" :span="12">
        <el-form-item :label="$t('标的名称')" prop="subjectName">
          <el-input v-model="bondForm.subjectName" :placeholder="$t('标的名称')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- nameCreditorRight	债权名称 -->
      <el-col v-if="hidePrivate.includes('nameCreditorRight')" :span="12">
        <el-form-item :label="$t('债权名称')" prop="nameCreditorRight">
          <el-input v-model="bondForm.nameCreditorRight" :placeholder="$t('债权名称')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- fundManager	基金管理人 -->
      <el-col v-if="hidePrivate.includes('fundManager')" :span="12">
        <el-form-item :label="$t('基金管理人')" prop="fundManager">
          <el-input v-model="bondForm.fundManager" :placeholder="$t('基金管理人')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- fundSize	基金规模 -->
      <el-col v-if="hidePrivate.includes('fundSize')" :span="12">
        <el-form-item :label="$t('基金规模')" prop="fundSize">
          <el-input v-model="bondForm.fundSize" :placeholder="$t('基金规模')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- termInvestment	投资期限 -->
      <el-col v-if="hidePrivate.includes('termInvestment')" :span="12">
        <el-form-item :label="$t('投资期限')" prop="termInvestment">
          <el-input v-model="bondForm.termInvestment" :placeholder="$t('投资期限')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- industryLevelOne	行业一级 -->
      <el-col v-if="hidePrivate.includes('industryLevelOne')" :span="6">
        <el-form-item :label="$t('行业(BClASS)')" prop="industryLevelOne">
          <el-input v-model="bondForm.industryLevelOne" :placeholder="$t('一级行业')" clearable show-word-limit :maxlength="100" />
          <span style="float: right;margin: -1px -18px 0 0px;">-</span>
        </el-form-item>
      </el-col>
      <!-- industryLevelTwo	行业二级 -->
      <el-col v-if="hidePrivate.includes('industryLevelTwo')" :span="6">
        <el-form-item :label="''" prop="industryLevelTwo">
          <el-input v-model="bondForm.industryLevelTwo" style="margin-top: 36px;" :placeholder="$t('二级行业')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- series	系列 -->
      <el-col v-if="hidePrivate.includes('series')" :span="12">
        <el-form-item :label="$t('系列')" prop="series">
          <el-input v-model="bondForm.series" :placeholder="$t('系列')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- level	级别 -->
      <el-col v-if="hidePrivate.includes('level')" :span="12">
        <el-form-item :label="$t('级别')" prop="level">
          <el-input v-model="bondForm.level" :placeholder="$t('级别')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- bondType	债券类型 -->
      <el-col v-if="hidePrivate.includes('bondType')" :span="12">
        <el-form-item :label="$t('债券类型')" prop="bondType">
          <el-input v-model="bondForm.bondType" :placeholder="$t('债券类型')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- currency	投资币种 -->
      <el-col v-if="hidePrivate.includes('currency')" :span="12">
        <el-form-item :label="$t('投资币种')" prop="currency">
          <dictSelect
            v-model="bondForm.currency"
            :clearable="true"
            dictionary="currency"
            :placeholder="$t('请选择')"
          />
        </el-form-item>
      </el-col>
      <!-- dueDate	到期日 -->
      <el-col v-if="hidePrivate.includes('dueDate')" :span="12">
        <el-form-item :label="$t('到期日')" prop="dueDate">
          <el-date-picker
            v-model="bondForm.dueDate"
            type="date"
            :placeholder="$t('选择日期')"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            :clearable="true"
          />
        </el-form-item>
      </el-col>
      <!-- repaymentOrder	偿付顺序 -->
      <el-col v-if="hidePrivate.includes('repaymentOrder')" :span="12">
        <el-form-item :label="$t('偿付顺序')" prop="repaymentOrder">
          <dictSelect
            v-model="bondForm.repaymentOrder"
            :clearable="true"
            dictionary="deal_repayment_order"
            :placeholder="$t('请选择')"
          />
        </el-form-item>
      </el-col>
      <!-- structuredType	结构化产品类别 -->
      <el-col v-if="hidePrivate.includes('structuredType')" :span="12">
        <el-form-item :label="$t('结构化产品类别')" prop="structuredType">
          <dictSelect
            v-model="bondForm.structuredType"
            :clearable="true"
            dictionary="deal_bond_structured_type"
            :placeholder="$t('请选择')"
          />
        </el-form-item>
      </el-col>
      <!-- natureEnterprise	企业性质 -->
      <el-col v-if="hidePrivate.includes('natureEnterprise')" :span="12">
        <el-form-item :label="$t('企业性质')" prop="natureEnterprise">
          <dictSelect
            v-model="bondForm.natureEnterprise"
            :clearable="true"
            dictionary="deal_bond_nature_enterprise"
            :placeholder="$t('请选择')"
          />
        </el-form-item>
      </el-col>
      <!-- externalRatingIssuer	发行人评级（外评） -->
      <el-col v-if="hidePrivate.includes('externalRatingIssuer')" :span="12">
        <el-form-item :label="$t('发行人评级（外评）')" prop="externalRatingIssuer">
          <dictSelect
            v-model="bondForm.externalRatingIssuer"
            :clearable="true"
            dictionary="letter_rating"
            :placeholder="$t('请选择')"
          />
        </el-form-item>
      </el-col>
      <!-- nameGuarantor	担保人名称 -->
      <el-col v-if="hidePrivate.includes('nameGuarantor')" :span="12">
        <el-form-item :label="$t('担保人名称')" prop="nameGuarantor">
          <el-input v-model="bondForm.nameGuarantor" :placeholder="$t('担保人名称')" clearable show-word-limit :maxlength="100" />
        </el-form-item>
      </el-col>
      <!-- ratingOutsideGuarantor	担保人评级（外评） -->
      <el-col v-if="hidePrivate.includes('ratingOutsideGuarantor')" :span="12">
        <el-form-item :label="$t('担保人评级（外评）')" prop="ratingOutsideGuarantor">
          <dictSelect
            v-model="bondForm.ratingOutsideGuarantor"
            :clearable="true"
            dictionary="letter_rating"
            :placeholder="$t('请选择')"
          />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <div v-if="bondForm.primaryBondCategory === 'JW'">
      <div class="area-header">
        <span class="area-header-title">{{ $t('评级列表') }}</span>
      </div>
      <el-table :data="bondForm.dealBaseBondLevelList" border class="nowrapTable formTable">
        <el-table-column :label="$t('评级机构')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('评级机构') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ratingAgencies'">
              <el-input v-model="scope.row.ratingAgencies" :placeholder="$t('评级机构')" show-word-limit maxlength="100" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('评级生效日期')" align="center">
          <template slot="header" slot-scope="{}">
            <!-- <i style="color: #ff4949;margin-right: 4px;">*</i>-->
            <span>{{ $t('评级生效日期') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'ratingEffectiveDate'">
              <el-date-picker
                v-model="scope.row.ratingEffectiveDate"
                type="date"
                :placeholder="$t('选择日期')"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                :clearable="true"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('最新主体评级')" align="center">
          <template slot="header" slot-scope="{}">
            <!-- <i style="color: #ff4949;margin-right: 4px;">*</i>-->
            <span>{{ $t('最新主体评级') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'latestSubjectRating'">
              <dictSelect
                v-model="scope.row.latestSubjectRating"
                :clearable="true"
                dictionary="letter_rating"
                :placeholder="$t('请选择')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('最新债项评级')" align="center">
          <template slot="header" slot-scope="{}">
            <!-- <i style="color: #ff4949;margin-right: 4px;">*</i> -->
            <span>{{ $t('最新债项评级') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'latestDebtRating'">
              <dictSelect
                v-model="scope.row.latestDebtRating"
                :clearable="true"
                dictionary="letter_rating"
                :placeholder="$t('请选择')"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" fixed="right" align="center" width="80">
          <template slot-scope="scope">
            <el-button type="text" class="el-icon-delete" @click="delHandle(scope.$index)" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!otherBtnHide" class="tol">
        <el-button has-permi="deal:base" type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t("添加") }}</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { listDealBaseBond, getDealBaseBond } from '@/api/bond/dealBondProduct/dealBondProduct.js'
export default {
  name: 'DealBaseBondForm',
  props: {
    bondForm: {
      type: Object,
      default: undefined
    },
    otherBtnHide: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      hidePrivate: [],
      restaurants: [],
      bondCategoryTwoStatus: true
    }
  },
  watch: {
  },
  mounted() {
    this.$nextTick(() => {
      this.formatSecondary(this.bondForm.secondaryBondCategory)
    })
    // 加载所有债券编码
    this.loadBondCodeAll()
  },
  methods: {
    formatSecondary(val) {
      if (val) {
        var _this = this
        _this.hidePrivate = []
        _this.$nextTick(() => {
          if (_this.bondForm.primaryBondCategory === 'JW') {
            // 境外 - 利率债、投资级债、高收益债
            if (val === 'LLZ' || val === 'TZJZ' || val === 'GSYZ') {
              _this.hidePrivate = ['bondCode', 'bondName', 'financingSubject', 'obligor', 'countryRisk', 'industryLevelOne', 'industryLevelTwo', 'currency', 'repaymentOrder', 'dueDate']
            }
            // 境外- 结构化产品
            if (val === 'JGHCP') {
              _this.hidePrivate = ['structuredType', 'series', 'level', 'dueDate', 'industryLevelTwo', 'industryLevelOne', 'countryRisk', 'subjectName', 'obligor', 'financingSubject', 'bondName', 'bondCode']
            }
            // 境外-私募债
            if (val === 'SMZ') {
              _this.hidePrivate = ['obligor', 'bondCode', 'bondName', 'countryRisk', 'industryLevelOne', 'industryLevelTwo', 'currency', 'dueDate', 'repaymentOrder', 'externalRatingIssuer', 'nameGuarantor', 'ratingOutsideGuarantor']
            }
            // 境外-基金投资
            if (val === 'JJTZ') {
              _this.hidePrivate = ['termInvestment', 'fundSize', 'currency', 'industryLevelTwo', 'industryLevelOne', 'countryRisk', 'fundManager', 'bondName', 'bondCode']
            }
          } else {
            // 境内-利率债、信用债
            if (val === 'LLZ' || val === 'XYZ') {
              _this.hidePrivate = ['natureEnterprise', 'bondType', 'dueDate', 'industryLevelTwo', 'industryLevelOne', 'currency', 'countryRisk', 'obligor', 'financing_subject', 'bondName', 'bondCode']
            }
            // 境内-债券基金
            if (val === 'ZQJJ') {
              _this.hidePrivate = ['bondCode', 'bondName', 'fundManager', 'fundSize', 'termInvestment', 'obligor', 'countryRisk', 'industryLevelOne', 'industryLevelTwo', 'currency']
            }
          }
          // 境外 - 境内-委托投资
          if (val === 'WTTZ') {
            _this.hidePrivate = ['bondCode', 'bondName', 'thirdPartyPrincipal', 'countryRisk', 'industryLevelTwo', 'industryLevelOne', 'currency']
          }
        })
      } else {
        this.hidePrivate = []
      }
    },
    // 一级变化二级联动
    setBondCategory(val) {
      if (val) {
        this.bondCategoryTwoStatus = false
        this.bondForm.secondaryBondCategory = undefined
        this.hidePrivate = []
        this.$nextTick(() => {
          this.bondCategoryTwoStatus = true
        })
      }
    },
    // 选择债券编码后自动填充债券数据
    handleSelect(val) {
      console.log(val)
      this.bondForm.primaryBondCategory = val.primaryBondCategory
      this.bondForm.secondaryBondCategory = val.secondaryBondCategory
      this.bondForm.bondCode = val.bondCode
      this.bondForm.bondName = val.bondName
      this.bondForm.financingSubject = val.financingSubject
      this.bondForm.obligor = val.obligor
      this.bondForm.countryRisk = val.countryRisk
      this.bondForm.subjectName = val.subjectName
      this.bondForm.series = val.series
      this.bondForm.nameCreditorRight = val.nameCreditorRight
      this.bondForm.fundManager = val.fundManager
      this.bondForm.fundSize = val.fundSize
      this.bondForm.termInvestment = val.termInvestment
      this.bondForm.industryLevelOne = val.industryLevelOne
      this.bondForm.industryLevelTwo = val.industryLevelTwo
      this.bondForm.currency = val.currency
      this.bondForm.dueDate = val.dueDate
      this.bondForm.repaymentOrder = val.repaymentOrder
      this.bondForm.level = val.level
      this.bondForm.structuredType = val.structuredType
      this.bondForm.bondType = val.bondType
      this.bondForm.natureEnterprise = val.natureEnterprise
      this.bondForm.externalRatingIssuer = val.externalRatingIssuer
      this.bondForm.nameGuarantor = val.nameGuarantor
      this.bondForm.ratingOutsideGuarantor = val.ratingOutsideGuarantor
      this.bondForm.thirdPartyPrincipal = val.thirdPartyPrincipal
      // 获取资表数据
      this.bondForm.dealBaseBondLevelList = []
      getDealBaseBond(val.id).then(res => {
        console.log(res)
        if (res.data.dealBaseBondLevelList !== null && res.data.dealBaseBondLevelList.length > 0) {
          res.data.dealBaseBondLevelList.forEach(item2 => {
            item2.id = undefined
            this.bondForm.dealBaseBondLevelList.push(item2)
          })
        }
      })
    },
    // 获取所有债券编码
    loadBondCodeAll() {
      listDealBaseBond().then(res => {
        if (res.rows !== undefined && res.rows !== null && res.rows.length > 0) {
          this.restaurants = res.rows
        }
      })
    },
    // 债券输入框搜索
    querySearch(queryString, cb) {
      var restaurants = this.restaurants
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.bondCode.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    // 校验必填
    checkIsNull(rule, value, callback) {
      value = this.bondForm[rule.field]
      if ((value === undefined || value === null || value === '')) {
        callback(new Error(this.$t('必填信息')))
      } else {
        callback()
      }
    },
    // 删除
    delHandle(index) {
      this.bondForm.dealBaseBondLevelList.splice(index, 1)
    },
    // 新增评级列表数据
    addHandle() {
      const row = {
        ratingAgencies: undefined,
        ratingEffectiveDate: undefined,
        latestSubjectRating: undefined,
        latestDebtRating: undefined
      }
      if (!(this.bondForm.dealBaseBondLevelList instanceof Array)) {
        this.$set(this.bondForm, 'dealBaseBondLevelList', [])
      }
      this.$nextTick(() => {
        this.bondForm.dealBaseBondLevelList.push(row)
      })
    }
  }
}
</script>

<style scoped>

</style>
